<script>
	export default {
		onLaunch() {
			this.num = 0
			//国际化，设置当前语言
			if (this.vuex_locale) {
				this.$i18n.locale = this.vuex_locale;
				this.$u.api.lang({
					lang: this.vuex_locale
				});
			}
			if (!uni.getStorageSync('token')) {
				this.login()
			} else {
				this.checkToken()
			}
			this.getLogo()
		},
		data() {
			return {
				num: 0
			}
		},

		methods: {
			getLogo() {
				this.$u.api.logo.getLogo({

				}).then(res => {
					if (res.code == 200) {
						try {
							uni.removeStorageSync('avatar')
							uni.removeStorageSync('logo')
							uni.removeStorageSync('isMaster')
							uni.setStorageSync('avatar', res.data.avatar)
							uni.setStorageSync('logo', res.data.logo)
							uni.setStorageSync('isMaster', res.data.isMaster)
						} catch (e) {

						}
					}

				})
			},
			// 登录
			login() {
				const that = this;
				that.num++
				if (that.num < 10) {
					try {
						uni.login({
							success(res) {
								// #ifdef MP-TOUTIAO
								that.getDyToken(res.code);
								// #endif
							},
							fail() {
								// 失败重新登录
								that.login()
							}
						})
					} catch (e) {}
				} else {
					uni.showModal({
						title: "登录失败",
						content: '请重启小程序或重启抖音',
						showCancel: false,
						confirmText: '知道了'
					});
				}
			},
			// 获取token
			getDyToken(code) {
				this.$u.api.dy.getToken({
					code: code,
				}).then(res => {
					if (res.code == 200) {
						if (res.data.token) {
							try {
								uni.removeStorageSync('token')
								uni.removeStorageSync('userInfo')
								uni.setStorageSync('token', res.data.token);
							} catch (e) {
							}

						} else {
							this.login()
						}
					} else {
						// 获取token失败 重新登录
						this.login()
					}
				})
			},
			// 校验token
			checkToken() {
				this.$u.api.dy.checkToken().then(res => {
					if (res.code == 401) {
						// 重新登录
						this.login()
					}
				})
			}
		},
	}
</script>
<style>
	@import url("~@/static/iconfont/iconfont.css");
</style>
<style lang="scss">
	@import "uview-ui/index.scss";
	@import "pages/common/aidex.scss";

	.bg-set {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		z-index: -1;

		background: #0F0F1C;
	}

	/deep/.u-empty {
		margin: 20px;
	}

	/deep/.u-empty .u-icon__img {
		width: 388rpx !important;
		height: 340rpx !important;
	}

	/deep/ .u-mode-center-box {
		background: none !important;
		overflow: initial !important;
	}

	.close {
		width: 65rpx;
		height: 65rpx;
		display: block;
		position: fixed;
		top: 127rpx;
		right: 41rpx;
		z-index: 999;
	}
</style>